#include"ringqueue.hpp"
#include"Task.hpp"
#include<unistd.h>
#include<pthread.h>
#include<ctime>
#include<sys/types.h>
#include<stdlib.h>
#include<string>
using namespace std;

void* Productor(void* rq)
{
    Ringqueue<CalTask> *q = static_cast<Ringqueue<CalTask>*>(rq);

    while(true)
    {
        int x = rand() % 100 + 1;
        int y = rand() % 10 + 1;
        char op = oper[rand()%oper.size()];
        CalTask t(x,y,op,mymath);

        q->push(t);
        cout<<"生产者派送一个任务: "<<t.toTaskstring(); 
        //sleep(1);
    }
}

void* consumer(void* rq)
{
    Ringqueue<CalTask> *q = static_cast<Ringqueue<CalTask>*>(rq);

    while(true)
    {
        sleep(3);

        CalTask t;
        q->pop(&t);
        string res = t();
        cout<<"消费者消费了一个任务: "<<res;
    }
    
}

int main()
{
    srand((unsigned int)time(nullptr)^getpid()^pthread_self());
    Ringqueue<CalTask> *rq = new Ringqueue<CalTask>();

    pthread_t p,c;
    pthread_create(&p,nullptr,Productor,rq);
    pthread_create(&c,nullptr,consumer,rq);
    
    pthread_join(p,nullptr);
    pthread_join(c,nullptr);


    //pthread_t p[4],c[8];
    // for(int i=0;i<4;i++) pthread_create(p+i,nullptr,Productor,rq);
    // for(int i=0;i<8;i++) pthread_create(c+i,nullptr,consumer,rq);

    // for(int i=0;i<4;i++) pthread_join(p[i],nullptr);
    // for(int i=0;i<8;i++) pthread_join(c[i],nullptr);

    delete rq;
    return 0;
}